﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DTcms.BLL
{
    /// <summary>
    /// 2021.01.15
    /// </summary>
    public class Data
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data dal;

        public Data()
        {
            dal = new DAL.Data();
        }
        public Data(bool isPrefix)
        {
            if (isPrefix)
            {
                dal = new DAL.Data(sysConfig.sysdatabaseprefix);
            }
            else
            {
                dal = new DAL.Data();
            }
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>返回的结果</returns>
        public bool Exists<T>(Expression<Func<T, bool>> whereExpression)
        {
            return dal.Exists<T>(whereExpression);
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>返回的结果</returns>
        public bool Exists<T>(int id)
        {
            return dal.Exists<T>(id);
        }

        /// <summary>
        /// 数量
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数量</returns>
        public int Count<T>(Expression<Func<T, bool>> whereExpression)
        {
            return dal.Count<T>(whereExpression);
        }

        /// <summary>
        /// 合计
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>合计数量</returns>
        public object Sum<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> expression)
        {
            return dal.Sum<T>(whereExpression, expression);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="model">数据</param>
        /// <returns>传入实体添加自增列</returns>
        public int Insert<T>(T model) where T : class, new()
        {
            return dal.Insert<T>(model);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="list">数据集合</param>
        /// <returns>影响的行数</returns>
        public int Insert<T>(List<T> list) where T : class, new()
        {
            return dal.Insert(list);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="model">实体</param>
        /// <returns>结果</returns>
        public bool Update<T>(T model) where T : class, new()
        {
            return dal.Update<T>(model);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="list">数据集合</param>
        /// <returns>影响的行数</returns>
        public int Update<T>(List<T> list) where T : class, new()
        {
            return dal.Update<T>(list);
        }

        /// <summary>
        /// 更新字段
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="expression">it => it.Num== it.Num+1</param>
        /// <param name="whereExpression">条件筛选</param>
        /// <returns>执行结果</returns>
        public bool UpdateColumns<T>(Expression<Func<T, T>> expression, Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            return dal.UpdateColumns<T>(expression, whereExpression);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns></returns>
        public bool Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            return dal.Delete<T>(whereExpression) > 0;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键</param>
        /// <returns>返回结果</returns>
        public bool Delete<T>(int id) where T : class, new()
        {
            return dal.Delete<T>(id);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression)
        {
            return dal.Get<T>(whereExpression);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="id">主键值</param>
        /// <returns>数据</returns>
        public T Get<T>(int id) 
        {
            return dal.Get<T>(id);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            return dal.Get<T>(whereExpression, orderExpression);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression)
        {
            return dal.List<T>(whereExpression);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, string strWhere)
        {
            return dal.List<T>(whereExpression, strWhere);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(string strWhere)
        {
            return dal.List<T>(strWhere);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            return dal.List<T>(whereExpression, orderExpression);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            return dal.List<T>(whereExpression, strWhere, orderExpression, orderByType);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(string strWhere, string strOrder)
        {
            return dal.List<T>(strWhere, strOrder);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression)
        {
            return dal.List<T>(top, whereExpression, orderExpression);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression)
        {
            return dal.List<T>(top, whereExpression, strWhere, orderExpression);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            return dal.List<T>(top, whereExpression, strWhere, orderExpression, orderByType);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, string strWhere, string strOrder)
        {
            return dal.List<T>(top, strWhere, strOrder);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, out int recordCount)
        {
            return dal.List<T>(pageSize, pageIndex, whereExpression, orderExpression, out recordCount);
        }

        // <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, out int recordCount)
        {
            return dal.List<T>(pageSize, pageIndex, whereExpression, strWhere, orderExpression, out recordCount);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strOrder">排序条件</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, string strWhere, string strOrder, out int recordCount)
        {
            return dal.List<T>(pageSize, pageIndex, strWhere, strOrder, out recordCount);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据</returns>
        public T Get<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            return dal.Get<T>(whereExpression, orderExpression, orderByType);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            return dal.List<T>(whereExpression, orderExpression, orderByType);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="top">前N条</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int top, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType)
        {
            return dal.List<T>(top, whereExpression, orderExpression, orderByType);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType, out int recordCount)
        {
            return dal.List<T>(pageSize, pageIndex, whereExpression, orderExpression, orderByType, out recordCount);
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="pageSize">每页数量</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="whereExpression">筛选条件</param>
        /// <param name="strWhere">其他筛选条件</param>
        /// <param name="orderExpression">排序条件</param>
        /// <param name="orderByType">排序类型</param>
        /// <param name="recordCount">返回数量</param>
        /// <returns>数据集</returns>
        public List<T> List<T>(int pageSize, int pageIndex, Expression<Func<T, bool>> whereExpression, string strWhere, Expression<Func<T, object>> orderExpression, SqlSugar.OrderByType orderByType, out int recordCount)
        {
            return dal.List<T>(pageSize, pageIndex, whereExpression, strWhere, orderExpression, orderByType, out recordCount);
        }

    }
}
